<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class StoreCompany extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_store_company';


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:17
     * @功能说明:列表
     */
    public function tmpList($dis,$page){

        $data = $this->where($dis)->order('top desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:18
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:20
     * @功能说明:
     */
    public function dataDel($dis){

        $res = $this->where($dis)->delete();

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:44
     * @功能说明:
     */
    public function getCompanyId($data){

        if($data['public_company']==1){

            $dis = [

                'status' => 1,

                'uniacid'=> $data['uniacid']
            ];

            $res = $this->getCompanyIds($dis);
        }else{

            $dis = [

                'a.tmp_id' => $data['id'],

                'a.uniacid'=> $data['uniacid']
            ];

            $res = $this->getId($dis);

        }

        return $res;

    }



    /**
     * @param $id
     * 回显
     */
    public function reset($id,&$data=array()){
        $cate   = Db::name('longbing_card_company')->where(['id'=>$id])->find();
        if(!empty($cate)&&$cate['pid']!=$cate['id']){
            array_unshift($data,$cate['id']);
            $this->reset($cate['pid'],$data);
        }
        return $data;
    }


    /**
     * @param $dis
     * @return array
     * 获取company
     */

    public function getId($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_company b','a.company_id=b.id')
            ->where($dis)
            ->column('company_id');
        $company_id = [];
        if(!empty($data)){
            foreach ($data as $v){
                $company_id[] = $this->reset($v);
            }
        }
        return array_values($company_id);
    }


}